package mushystuff.dtb.web.components;

import java.util.SortedSet;

import mushystuff.dtb.core.model.Product;
import mushystuff.dtb.core.model.Sprint;
import mushystuff.dtb.core.service.ProductService;

import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

@SuppressWarnings("unused")
@IncludeJavaScriptLibrary({"${tapestry.scriptaculous}/prototype.js", "context:/js/livepipe.js", "context:/js/tabs.js"})
public class SprintList {
    
    @Parameter(required=true)
    private SortedSet<Sprint> sprints;
    
    @Parameter(allowNull=false)
    @Property
    private Product product;
    
    @Property
    private Sprint[] sprintsArray;
    
    @Property
    private Sprint sprint;
    
    @Property
    private Sprint newSprint;
    
    @Inject
    private ProductService productService;
    
    @Environmental
    private RenderSupport renderSupport;
    
    @SetupRender
    void copyToArray() {
        sprintsArray = new Sprint[sprints.size()];
        sprintsArray = (Sprint[])sprints.toArray(sprintsArray);
        if (product != null) {
            newSprint = new Sprint();
        }
        renderSupport.addScript("new Control.Tabs('tabs_sprints');");
    }
    
    @OnEvent(component="newSprint", value="success")
    void saveSprint() {
        productService.addSprint(product, newSprint);
        newSprint = new Sprint();
    }

}
